package life;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// 根据指定长度拆分数组
public class SplitArr {

    public static void main(String[] args) {

        String s = "1,2,3,4,5,6,7,8,9,10,11,12,13";
        System.out.println(splitArr(s.split(","),4));
    }

    private static  List<String[]> splitArr(String[] array, int num) {
        List<String[]> res1 = new ArrayList<>();
        List<List<String>> res = new ArrayList<>();
        int count = array.length % num == 0 ? array.length / num : array.length / num + 1;
        for (int i = 0; i < count; i++) {
            int index = i * num;
            int j = 0;
            ArrayList<String> list = new ArrayList<>();
//            new String[]();
            while (j < num && index < array.length) {
                list.add(array[index++]);
                j++;
            }
            res.add(list);
        }

        for (List<String> re : res) {
            res1.add(re.toArray(new String[]{}));

        }
        String[][] strings = res1.toArray(new String[][]{});
        return res1;
    }
}
